const dayjs = require('dayjs')
const db = require('../config/dbConfig')
class User{
    constructor(userInfo) {
        this.userInfo = userInfo
    }
    async register() {
        const { userName, role, password } = this.userInfo;
        const findUserSql = "SELECT * FROM user WHERE userName = ?";
        const data = await db.execute(findUserSql, [userName]);
        if (data[0].length > 0) {
            return {
                code: 402,
                msg: "用户名重复，用户已存在",
            };
        }
        const createTime = dayjs().format("YYYY-MM-DD HH:mm:ss");
        const addUserSql = "INSERT INTO user (userName, role, password, createTime) VALUES (?, ?, ?, ?)";
        const Info = await db.execute(addUserSql, [
            userName,
            role,
            password,
            createTime,
        ]);
        return {
            code: 200,
            msg: "注册成功",
            data: {
                userName,
                role,
            },
        };
    }
    static login(userName) {
        const sql = "SELECT * FROM user WHERE userName = ?";
        return db.execute(sql,[userName])
    }
    static getUserInfo(userId) {
        const sql = "SELECT * FROM user WHERE id = ?";
        return db.execute(sql, [userId]);
    }
}
module.exports = User;
